package com.chencs.biz.product.controller;

import com.alibaba.fastjson.JSON;
import com.chencs.biz.product.service.ProductService;
import com.chencs.vo.biz.product.FavoritesModel;
import com.chencs.vo.common.grant.RoleModel;
import framework.model.ResponseOptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by fangzehua on 2017/6/30.
 */
@RequestMapping("/product")
@RestController
public class ProductController {

    @Autowired
    ProductService productService;
    @RequestMapping(value = "/doFavoriteList", method = RequestMethod.GET)
    public ModelAndView doFavoriteList() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/biz/product/favorite_list");
        return modelAndView;
    }


    @RequestMapping(value = "/favoriteList", method = RequestMethod.GET)
    public String list() {
        String retStr = "";
        try {
            retStr = productService.getList();
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }




    @RequestMapping(value = "/clear_pro", method = RequestMethod.GET)
    public String clear_pro() {
        String retStr = "";
        try {
            retStr = productService.clear_pro();
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }





    @RequestMapping(value = "/doEditor/{code}", method = RequestMethod.GET)
    public ModelAndView doRolePage(@PathVariable("code") String code , Model model) {
        FavoritesModel favoritesModel = null;
        try{
            favoritesModel = JSON.parseObject(productService.findFavoritesModelByCode(code+""),FavoritesModel.class);
        }catch (Exception e){
            e.printStackTrace();
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/biz/product/favorite_editor");
        model.addAttribute("favoritesModel",favoritesModel);
        return modelAndView;
    }





    @RequestMapping(value = "/updateFavoritesByModel", method = RequestMethod.POST)
    public String updateFavoritesByModel(@RequestBody  FavoritesModel model) {
        try{
            productService.updateFavoritesByModel(model);
            return ResponseOptUtil.commonSucess();
        }catch (Exception e){
            e.printStackTrace();
            return  ResponseOptUtil.commonMsgError("修改收藏夹失败");
        }
    }

    @RequestMapping(value = "/syncFavorites", method = RequestMethod.GET)
    public String syncFavorites() {
        try{
            productService.syncFavorites();
            return ResponseOptUtil.commonSucess();
        }catch (Exception e){
            e.printStackTrace();
            return  ResponseOptUtil.commonMsgError("同步收藏夹");
        }
    }

    /**
     * 根据收藏夹同步商品列表
     * @param code
     * @return
     */
    @RequestMapping(value = "/getProductByCidForList", method = RequestMethod.POST)
    public String getProductByCidForList(@RequestBody String code) {
        String retStr = "";
        try {
            retStr =   productService.syncProductByFavorites(code);
            return retStr;
        }catch (Exception e){
            e.printStackTrace();
            return  ResponseOptUtil.commonMsgError("同步商品失败");
        }
    }


    /**
     * 获取商品列表
     * @return
     */
    @RequestMapping(value = "/doProductList", method = RequestMethod.GET)
    public ModelAndView doProductList() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/biz/product/product_list");
        return modelAndView;
    }


    /**
     * 获取商品列表
     * @return
     */
    @RequestMapping(value = "/productList", method = RequestMethod.GET)
    public String productList() {
        try {

            return productService.getProduct();
        }catch (Exception e){
            return  ResponseOptUtil.commonMsgError("商品列表失败");
        }

    }








}
